//1.typeof
function printId(id: number | string) {
  //这里一定是字符串类型
  if (typeof id === "string") {
  } else {
  }
}
printId(123);
printId("123");

//2.平等缩小的判断(一般判断字面量类型)
type Direaction = "left" | "right" | "up" | "down";

function switchDireaction(direaction: Direaction) {
  if (direaction === "left") {
    console.log("向左移动");
  } else if (direaction === "right") {
    console.log("向右移动");
  }
}

//传入一个日期，打印日期，别人传来坑是字符串，也可能是Date类型
function prinitDate(date: string | Date) {
  //判断date是不是Date的一个实例
  if (date instanceof Date) {
    //这是日期类型
    console.log(date.getTime());
  } else {
    //这是字符串
    console.log(date);
  }
}

//判断是否有一个属性
interface isSwim {
  swim: () => void;
}

interface isRun {
  run: () => void;
}

//in操作符
function move(animal: isSwim | isRun) {
  //判断swim是否在anmal中
  if ("swim" in animal) {
    animal.swim();
  } else {
    animal.run();
  }
}

const fish: isSwim = {
  swim: function () {},
};
const dog: isRun = {
  run: function () {},
};
121;
move(fish);
move(dog);
